package org.joy.db2;

import java.io.Serializable;

public class ResultEntry implements Serializable, Comparable<ResultEntry> {
    private final static String NOT_AVALIABLE = "NOT AVAILABLE NOW!";
    private String snippet;
    private String URL;
    private double score;
    private String title;

    public ResultEntry(SearchEntry se, DocumentEntry doc) {

	this.URL = doc.getURL();
	this.score = se.getScore();
	if (doc.getParagraphs().size() == 0)
	    return;
	this.title = doc.getParagraphs().get(0);
	System.out.println(se.getProx());
	String content = doc.getDocument();
	// get snippet from content and prox pos;
	int start = se.getSnippetPos()[0], end = se.getSnippetPos()[1];

	int stride = 20, maxStride = 50;
	if (start == -1 || end == -1) {
	    snippet = "Not Available";
	    return;
	}
	start = start - stride > 0 ? start - stride : 0;
	end = end + stride > content.length() ? content.length() : end + stride;
	if (end - start > maxStride) {
	    end = start + maxStride;
	}
	snippet = content.substring(start, end).replaceAll("\\n+|\\r+", " ");
    }

    public ResultEntry(String line) {
	String[] seg = line.split("\t");
	URL = seg[0];
	title = seg[1];
	score = Double.parseDouble(seg[2]);
	snippet = seg[3];
    }

    public double getScore() {
	return score;
    }

    public String getSnippet() {
	return snippet;
    }

    public String getURL() {
	return URL;
    }

    public String getTitle() {
	return title;
    }

    @Override
    public String toString() {
	return URL + "\t" + title + "\t" + score + "\t" + snippet;
    }

    @Override
    public int compareTo(ResultEntry o) {
	return Double.compare(this.score, o.score);
    }
}
